package com.webdemo.util.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/*
 * 连接数据库的工具类
 */
public class DBUtil {

	public static final String URL = "jdbc:mysql://127.0.0.1:3306/test";
	public static final String USER = "root";
	public static final String PASSWORD = "root";
	private static Connection conn = null;

	/*
	 * 静态加载数据库驱动 获得数据库连接
	 */
	static {
		// 加载驱动程序
		try {
			Class.forName("com.mysql.jdbc.Driver");
			// 获取数据库连接
			conn = DriverManager.getConnection(URL, USER, PASSWORD);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 返回数据库连接
	public static Connection getConn() {
		return conn;
	}
}
